////
//
/// \file ./include/inc/parser/parse_tree_node.hpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#ifndef inc_parser_parse_tree_node_header
#define inc_parser_parse_tree_node_header

#include <string>

namespace inc {
namespace parser {

/// \brief  Basic parse tree node
/// \author Chandler Carruth
/// \date   2007.06.15
///
/// This class provides the basic structure for a parse tree node, including
/// tree navigation: up, down, and across a set of siblings. It is abstract,
/// and must be derived to be useful, but provides an API for manipulating
/// generic parse trees.
///
/// TODO(chandlerc): Consider migrating to a DAG view of the source, as trees
/// are often space-inefficient DAGs, we might get some savings by having
/// multiple paths through the same node.
///
/// TODO(chandlerc): Consider replacing this with the equivalent code from BGL
/// for an intrusively linked tree or DAG.

class ParseTreeNode
{
public:
  /// \name Constructors and Destructors
  //@{

  /// \brief  Default constructor, clears all pointers
  ParseTreeNode()
      : parent(NULL), next(NULL), prev(NULL), front(NULL), back(NULL)
  {}

  virtual ~ParseTreeNode();

  //@}

  /// \name Parsing Interface
  //@{

  /// \brief Iterator type for parsing
  typedef std::string::const_iterator iterator;

  /// \brief Abstract interface implemented by all nodes
  virtual bool parse( const iterator& first,
                      const iterator& last,
                      iterator& last_parsed ) = 0;

  /// \brief Getter for the parsed text of a particular node
  virtual std::string get_text() const { return text; }

  //@}

  /// \name Tree Methods
  //@{

  virtual bool link( ParseTreeNode *p, ParseTreeNode *n = NULL );
  virtual bool unlink();

  //  sibling navigation
  virtual ParseTreeNode *get_next() { return next; }
  virtual const ParseTreeNode *get_next() const { return next; }
  virtual bool get_next( ParseTreeNode *&node )
      { node = get_next(); return node != NULL; }
  virtual bool get_next( const ParseTreeNode *&node ) const
      { node = get_next(); return node != NULL; }

  virtual ParseTreeNode *get_prev() { return prev; }
  virtual const ParseTreeNode *get_prev() const { return prev; }
  virtual bool get_prev( ParseTreeNode *&node )
      { node = get_prev(); return node != NULL; }
  virtual bool get_prev( const ParseTreeNode *&node ) const
      { node = get_prev(); return node != NULL; }

  //  parent navigation
  virtual ParseTreeNode *get_parent() { return parent; }
  virtual const ParseTreeNode *get_parent() const { return parent; }
  virtual bool get_parent( ParseTreeNode *&node )
      { node = get_parent(); return node != NULL; }
  virtual bool get_parent( const ParseTreeNode *&node ) const
      { node = get_parent(); return node != NULL; }

  //  child navigation
  virtual ParseTreeNode *get_front_child() { return front; }
  virtual const ParseTreeNode *get_front_child() const { return front; }
  virtual bool get_front_child( ParseTreeNode *&node )
      { node = get_front_child(); return node != NULL; }
  virtual bool get_front_child( const ParseTreeNode *&node ) const
      { node = get_front_child(); return node != NULL; }

  virtual ParseTreeNode *get_back_child() { return back; }
  virtual const ParseTreeNode *get_back_child() const { return back; }
  virtual bool get_back_child( ParseTreeNode *&node )
      { node = get_back_child(); return node != NULL; }
  virtual bool get_back_child( const ParseTreeNode *&node ) const
      { node = get_back_child(); return node != NULL; }

  //@}

protected:
  /// \brief Derived class setter for text
  virtual void set_text( const std::string &t ) { text = t; }

private:
  ParseTreeNode *parent;

  ParseTreeNode *next;
  ParseTreeNode *prev;

  ParseTreeNode *front;
  ParseTreeNode *back;

  std::string text;
};

} //  end parser namespace
} //  end inc namespace
#endif //  inc_parser_parse_tree_node_header
